package com.t21;

import static net.mindview.util.Print.print;

/**
 * Created by xuyiwei on 2018/9/16.
 */
class Sleeper extends Thread{
    private int duration;
    public Sleeper(String name,int sleepTime){super(name);duration = sleepTime;start();}
    public void run(){
        try {
            sleep(duration);
        }catch (InterruptedException e){
            print(getName()+" was interrupted. "+"isInterrupted(): "+isInterrupted());
        }
        print(getName()+" has awakened");
    }
}
class Joiner extends Thread{
    private Sleeper sleeper;
    public Joiner(String name,Sleeper sleeper){super(name);this.sleeper = sleeper ; start();}
    public void run(){
        try {
            sleeper.join();
        }catch (InterruptedException e){
            print("Interrupted");
        }
        print(getName()+ " join completed");
    }
}
public class Joining {
    public static void main(String[] args) {
        Sleeper sleepy = new Sleeper("Sleepy",15000);
        Sleeper grumpy = new Sleeper("Grumpy",15000);
        Joiner dopey = new Joiner("dopey",sleepy);
        Joiner doc = new Joiner("doc",grumpy);
        grumpy.interrupt();
    }
    /**
     * Grumpy was interrupted. isInterrupted(): false
     Grumpy has awakened
     doc join completed
     Sleepy has awakened
     dopey join completed
     */
}
